package com.aps.common.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

/**
 * 通用返回结果实体
 * 
 * @param <T> 数据类型
 * @author APS Scaffold
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "通用返回结果实体")
public class Result<T> {
    
    /**
     * 状态码
     */
    @Schema(description = "状态码", example = "200")
    private Integer code;

    /**
     * 返回主题 (业务异常使用，对应前端弹出dialog 的标题)
     */
    private String topic;


    /**
     * 返回消息
     */
    @Schema(description = "返回消息", example = "操作成功")
    private String message;
    
    /**
     * 返回数据
     */
    @Schema(description = "返回数据")
    private T data;
    
    /**
     * 时间戳
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "时间戳", example = "2024-01-01 12:00:00")
    private LocalDateTime timestamp;
    
    /**
     * 是否成功
     */
    @Schema(description = "是否成功", example = "true")
    private Boolean success;
    
    /**
     * 成功返回结果
     */
    public static <T> Result<T> success() {
        return success(null);
    }
    
    /**
     * 成功返回结果
     */
    public static <T> Result<T> success(T data) {
        return success(data, "操作成功");
    }
    
    /**
     * 成功返回结果
     */
    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage(message);
        result.setData(data);
        result.setTimestamp(LocalDateTime.now());
        result.setSuccess(true);
        return result;
    }
    
    /**
     * 失败返回结果
     */
    public static <T> Result<T> error() {
        return error(500, "操作失败");
    }
    
    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(String message) {
        return error(500, message);
    }
    
    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        result.setTimestamp(LocalDateTime.now());
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(Integer code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setTimestamp(LocalDateTime.now());
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> error(Integer code, String message, T data , String topic) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setTimestamp(LocalDateTime.now());
        result.setTopic(topic);
        result.setSuccess(false);
        return result;
    }

    /**
     * 校验失败返回前端
     * @param message 信息标题
     * @param data 涉及的数据
     * @param title 前端弹窗标题
     * @param errorLog 错误日志
     * @return
     * @param <T>
     */
    public static <T> Result<T> invalidateError(String message, T data , String title , String errorLog) {
        Result<T> result = new Result<>();
        result.setCode(499);
        result.setMessage(message);
        result.setData(data);
        result.setTimestamp(LocalDateTime.now());
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> invalidateError(String message, T data) {
        return invalidateError(message,data,"参数校验错误",null);
    }
}
